package com.ihr360.commons.model.type;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.ihr360.commons.model.IEnumData;
import com.ihr360.commons.model.annotation.EnumData;
import com.ihr360.commons.serializer.jackson.EnumDataSerializer;

import java.lang.reflect.Method;

@EnumData
@JsonSerialize(using = EnumDataSerializer.class)
public enum RoundingMode implements IEnumData {
    PRIMITIVE("原始数据"), ROUND("四舍五入"), CEIL("向上舍入数字"), FLOOR("向下舍入数字");

    private final String title;

    RoundingMode(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    @JsonCreator
    public static RoundingMode factory(@JsonProperty(value = "id") String id) {
        return RoundingMode.valueOf(id);
    }

    public java.math.RoundingMode getJavaRoundingMode() {
        switch (this) {
            case CEIL:
                return java.math.RoundingMode.CEILING;
            case FLOOR:
                return java.math.RoundingMode.FLOOR;
            default:
                return java.math.RoundingMode.HALF_UP;
        }
    }

    public double execute(Number number) throws Exception {
        return execute(number.doubleValue());
    }

    public double execute(double number) throws Exception {
        Method method = Math.class.getMethod(toString().toLowerCase(), double.class);
        return (double) method.invoke(Math.class, number);
    }

    public double execute(long number) throws Exception {
        return execute((double) number);
    }

    public double execute(int number) throws Exception {
        return execute((double) number);
    }

    public double execute(short number) throws Exception {
        return execute((double) number);
    }

    public double execute(byte number) throws Exception {
        return execute((double) number);
    }
}